<template>
  <div class="resetpassword">
    <div class="reset">
      <div class="title">
        <p>
          <span>密码修改</span>
          <router-link to="/home/set/userset">返回个人设置</router-link>
        </p>
      </div>
      <div class="form">
        <div>
          <p>您当前的密码</p>
          <input v-model="nowPassword" type="password" />
        </div>
        <div>
          <p>新密码</p>
          <input v-model="newPassword" type="password" />
        </div>
        <div>
          <p>确认密码</p>
          <input v-model="surePassword" type="password" />
        </div>
        <div>
          <button @click="myReset">更新密码</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "resetpassword",
  data() {
    return {
      nowPassword: "",
      newPassword: "",
      surePassword: "",
    };
  },
  methods: {
    myReset() {
      if (
        this.newPassword.length == 0 ||
        this.nowPassword.length == 0 ||
        this.surePassword.length == 0
      ) {
        alert("请输入密码");
        return false;
      }
      let nowpassword = JSON.parse(localStorage.getItem("userMsg")).password;

      // console.log(nowpassword);
      if (this.nowPassword != nowpassword) {
        alert("旧密码输入有误");
        return false;
      }
      if (this.newPassword == nowpassword) {
        alert("新密码与旧密码不能相同");
        return false;
      }
      if (this.newPassword != this.surePassword) {
        alert("两次密码输入不一致");
        return false;
      }
      if (
        this.newPassword == this.surePassword &&
        this.newPassword != this.nowPassword
      ) {
        if (this.newPassword.length < 7) {
          alert("密码长度过低，请重新输入");
        } else {
          alert("密码更新成功");
          let obj = JSON.parse(localStorage.getItem("userMsg"));
          obj.password = this.newPassword;
        //   let useremail = JSON.parse(localStorage.getItem("userMsg")).email;
        //   let obj = { email: useremail, password: this.newPassword };
          localStorage.setItem("userMsg",JSON.stringify(obj))
          this.$store.state.userPassword = this.newPassword;
          this.$router.push({ path: "/home/set/userset" });
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.resetpassword {
  .reset {
    .title {
      margin: 15px 0px;
      p {
        span {
          color: #808080;
          font-size: 20px;
          margin-right: 20px;
        }
        a {
          color: #34495e;
          font-weight: bold;
          font-size: 16px;
        }
      }
    }
    .form {
      div {
        margin-bottom: 15px;
        p {
          margin-bottom: 10px;
        }
        input {
          border: 1px solid #ccc;
          border-radius: 5px;
          width: 220px;
          height: 35px;
        }
        button {
          font-size: 16px;
          padding: 5px 15px;
          border: 1px solid #ccc;
          border-radius: 5px;
          color: #808080;
          cursor: pointer;
          &:active {
            color: #000;
          }
        }
      }
    }
  }
}
</style>